package com.dom.domweb.modular.controller.sysmger;

import com.dom.common.constants.ConstantError;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.service.ITMenuInfoService;
import com.dom.domweb.model.MenuNode;
import com.dom.domweb.modular.service.MenuTreeService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.util.ShiroUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 菜单控制层代码逻辑
 * @Author xllei
 * @Date 2018/11/22 11:25 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/sys")
public class MenuController {
    @Autowired
    private MenuTreeService menuTreeService;
    @Autowired
    private ITMenuInfoService menuInfoService;

    @RequestMapping("/loadMenuTree")
    public LayuiModel loadMenuTree(Integer roleId){
        ShiroUser user = ShiroUtil.getUser();
        List<MenuNode> nodes = menuTreeService.loadMenuTree(user, roleId);
        return new LayuiModel(nodes);
    }

    @RequestMapping("/getRootList")
    public List<MenuNode> getRootList(){
        return menuTreeService.getRootList();
    }


    @RequestMapping("/editMenuInfo")
    public RspModel editMenuInfo(@RequestParam("menuId") Integer menuId, @RequestParam("menuName") String menuName,
                                 @RequestParam("menuType") Integer menuType, @RequestParam("menuUrl") String menuUrl,
                                 @RequestParam("menuDesc") String menuDesc, @RequestParam("menuNo") Integer menuNo,
                                 @RequestParam("fatherMenuId") Integer fatherMenuId, @RequestParam("stat") Integer stat){
        B212R request = new B212R();

        request.setMenuId(menuId);
        request.setMenuName(menuName);
        request.setMenuType(menuType);
        request.setMenuUrl(menuUrl);
        request.setMenuDesc(menuDesc);
        request.setMenuNo(menuNo);
        request.setFatherMenuId(fatherMenuId);
        request.setStat(stat);

        B212A response = menuInfoService.updateMenuInfo(request);
        return new RspModel(response.getMenuId());
    }

    @RequestMapping("/delMenuInfo")
    public RspModel delMenuInfo(@RequestParam("menuId") Integer menuId){
        B211R request = new B211R();

        if(ObjectUtil.isNull(menuId)){
            return new RspModel(ConstantError.ILLREQERROR);
        }
        request.setMenuId(menuId);

        B211A response = menuInfoService.delMenuInfo(request);

        return new RspModel(response.getMenuId());
    }

    @RequestMapping("/addMenuInfo")
    public RspModel addMenuInfo(@RequestParam("menuName") String menuName,
                                @RequestParam("menuType") Integer menuType, @RequestParam("menuUrl") String menuUrl,
                                @RequestParam("menuDesc") String menuDesc, @RequestParam("menuNo") Integer menuNo,
                                @RequestParam("fatherMenuId") Integer fatherMenuId, @RequestParam("stat") Integer stat){
        B210R request = new B210R();

        request.setMenuName(menuName);
        request.setMenuType(menuType);
        request.setMenuUrl(menuUrl);
        request.setMenuDesc(menuDesc);
        request.setMenuNo(menuNo);
        request.setFatherMenuId(fatherMenuId);
        request.setStat(stat);
        B210A response = menuInfoService.addMenuInfo(request);

        return new RspModel(response.getMenuId());
    }

    @Data
    public class LayuiModel{
        private Status status;
        private Object data;

        public LayuiModel(Object data){
            status = new Status();
            status.code = 200;
            status.message = "交易成功";
            this.data = data;
        }

        @Data
        public class Status{
            private Integer code;
            private String message;
        }
    }
}
